﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Master_Warehouse : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterWarehouse bll = new MasterWarehouseBLL();
        IBLLTree bllTree = new TreeBLL();
        IBLLSysScope bllSysScope = new SysScopeBLL();
        IBLLHrmScope bllHrmScope = new HrmScopeBLL();
        IBLLMasterWarehouseConsume bllConsume = new MasterWarehouseConsumeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadScope();
                LoadJobSiteTree();
                GetKeyValue();
            }
        }

        protected void RadTabStrip1_TabClick(object sender, RadTabStripEventArgs e)
        {
            if (RadTabStrip1.SelectedTab.Value == "Detail")
            {
                GetKeyValue();
            }
            else if (RadTabStrip1.SelectedTab.Value == "JobSite")
            {
                LoadJobSiteConsumeGrid();
            }
        }

        public void GetKeyValue()
        {
            int warehouse_seq = Convert.ToInt32(Request.QueryString["Seq"]);
            MasterWarehouseInfo info = bll.GetMasterWarehouse(Convert.ToInt32(warehouse_seq));
            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");

            if (info.InfoJobSite.Seq.ToString().Length > 0)
            {
                RadTreeNode node = tree.FindNodeByValue(info.InfoJobSite.InfoScope.Seq.ToString() + "-" + info.InfoJobSite.Seq.ToString());
                node.Selected = true;
                if (tree.SelectedNode != null)
                {
                    cbJobSite.Text = tree.SelectedNode.Text;
                }
                else
                {
                    cbJobSite.Text = "";
                }
            }

            txtName.Text = info.Name;
            //txtDescr.Text = info.Descr;
            cbScope.SelectedValue = info.InfoScope.Seq.ToString();
            if (cbScope.SelectedValue == "")
            {
                cbScope.SelectedIndex = -1;
                cbScope.Text = "";
            }

            cmdFac.ViewCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
            lblJobSite.Text = info.InfoJobSite.Name;
        }

        public void LoadScope()
        {
            IList<SysScopeInfo> info = bllSysScope.GetSysScope();
            cbScope.DataSource = info;
            cbScope.DataBind();
        }

        public void LoadJobSiteTree()
        {
            IList<TreeInfo> info = bllTree.GetJobSiteTree();

            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");

            tree.DataSource = info;
            tree.DataBind();
        }

        public void LoadJobSiteConsumeGrid()
        {
            int warehouse_seq = Convert.ToInt32(Request.QueryString["Seq"]);
            Grid_JobSite.DataSource = bllConsume.GetMasterWarehouseConsumeWithParent(warehouse_seq);
            GridGroupByExpression ge = new GridGroupByExpression(Grid_JobSite.Columns[1]);
            Grid_JobSite.MasterTableView.GroupByExpressions.Add(ge);
            Grid_JobSite.DataBind();

            cmdFac.GridCommand(panelList_JobSite, panelGrid_JobSite, panelCommand_JobSite);
        }

        protected void Edit_Detail_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
        }

        protected void EditSave_Detail_Click(object sender, EventArgs e)
        {
            int warehouse_seq = Convert.ToInt32(Request.QueryString["Seq"]);
            MasterWarehouseInfo info = new MasterWarehouseInfo();
            SysScopeInfo infoScope = new SysScopeInfo();
            HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
            RecordInfo infoRecord = new RecordInfo();
            RadTreeView tree = (RadTreeView)cbJobSite.Items[0].FindControl("TreeViewJobSite");
            info.Seq = warehouse_seq;
            info.Name = txtName.Text;
            //info.Descr = txtDescr.Text;
            infoScope.Seq = (cbScope.SelectedValue.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(cbScope.SelectedValue));
            if (tree.SelectedNode != null)
            {
                string[] all_value = tree.SelectedNode.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                infoJobSite.Seq = Convert.ToInt32(all_value[1]);
            }
            infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;
            info.InfoJobSite = infoJobSite;
            info.InfoScope = infoScope;
            info.InfoRecord = infoRecord;

            if (ValtxtName.IsValid == false || ValcbScope.IsValid == false)
            {
                return;
            }

            try
            {
                bll.Update(info);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            GetKeyValue();
        }

        protected void Cancel_Detail_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList_Detail, panelGrid_Detail, panelCommand_Detail);
        }

        protected void AddNew_JobSite_Click(object sender, EventArgs e)
        {
            int warehouse_seq = Convert.ToInt32(Request.QueryString["Seq"]);
            IList<TreeInfo> infoList = bllTree.GetWarehouseConsumeJobSiteTree(warehouse_seq);
            JobsiteTree.DataSource = infoList;
            JobsiteTree.DataBind();
            JobsiteTree.ExpandAllNodes();

            cmdFac.AddNewCommand(panelList_JobSite, panelGrid_JobSite, panelCommand_JobSite);
        }

        protected void AddNewSave_JobSite_Click(object sender, EventArgs e)
        {
            int warehouse_seq = Convert.ToInt32(Request.QueryString["Seq"]);
            IList<MasterWarehouseConsumeInfo> infoList = new List<MasterWarehouseConsumeInfo>();
            foreach (RadTreeNode node in JobsiteTree.GetAllNodes())
            {
                if (node.Checked)
                {
                    string[] all_value = node.Value.Split(new string[] { "-" }, StringSplitOptions.None);
                    if (all_value.Length == 2)
                    {
                        MasterWarehouseConsumeInfo info = new MasterWarehouseConsumeInfo();
                        HrmJobSiteInfo infoJobSite = new HrmJobSiteInfo();
                        MasterWarehouseInfo infoWarehouse = new MasterWarehouseInfo();
                        RecordInfo infoRecord = new RecordInfo();
                        infoJobSite.Seq = Convert.ToInt32(all_value[1]);
                        infoWarehouse.Seq = warehouse_seq;
                        infoRecord.Created_by = cn.UserID;
                        infoRecord.Created_when = DateTime.Now;
                        info.InfoJobSite = infoJobSite;
                        info.InfoWarehouse = infoWarehouse;
                        info.InfoRecord = infoRecord;
                        infoList.Add(info);
                    }
                }
            }

            try
            {
                bllConsume.Insert(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            LoadJobSiteConsumeGrid();
        }

        protected void Delete_JobSite_Click(object sender, EventArgs e)
        {
            IList<MasterWarehouseConsumeInfo> infoList = new List<MasterWarehouseConsumeInfo>();
            foreach (GridDataItem data in Grid_JobSite.MasterTableView.GetSelectedItems())
            {
                MasterWarehouseConsumeInfo info = new MasterWarehouseConsumeInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllConsume.DeleteFromDB(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadJobSiteConsumeGrid();
        }

        protected void ShowGrid_JobSite_Click(object sender, EventArgs e)
        {
            LoadJobSiteConsumeGrid();
        }

        protected void Grid_JobSite_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadJobSiteConsumeGrid();
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid_JobSite.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid_JobSite.MasterTableView.FilterExpression = string.Empty;
            LoadJobSiteConsumeGrid();
        }
    }
}
